package com.itheima.system.controller;

import com.itheima.system.pojo.Question;
import com.itheima.system.service.SystemService;
import com.itheima.system.util.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  10:04 2020/6/23</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/6/23      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Controller
@RequestMapping("system")
@Api(tags = "添加问题controller")
public class SystemController {

    @Autowired
    private SystemService systemService;

    @ApiOperation("跳转至添加问题页面")
    @GetMapping("/toAddQuestion")
    public String toUpload() {
        return "addQuestion";
    }

    @ApiOperation("添加问题接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "问题标题"),
            @ApiImplicitParam(name = "answer", value = "问题答案")
    }
    )
    @PostMapping("/addQuestion")
    public String addQuestion(String title, String answer, Model model) {
        Question question = new Question();
        question.setTitle(title);
        question.setAnswer(answer);
        IdWorker idWorker = new IdWorker(0, 0);
        question.setId(idWorker.nextId());
        String result = systemService.addQuestion(question);
        model.addAttribute("result", question.getTitle() + result);
        return "result";
    }

}
